package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DataAccessObject;

public class LeagueEditor extends AutoTeamDialog
{
	private static final long serialVersionUID = 4898764547417325573L;

	private JButton changeButton = new JButton("Change");
	private JButton closeButton = new JButton("Cancel");
	private AutoTeamTableModel model;
	
	private int selectedRow;
	private String origLeague;
	private String origSport;
	
	
	private JTextField sportField;

	private JTextField leagueField;
	
	public LeagueEditor(JFrame dialogOwner)
	{
		super(dialogOwner);
		this.setTitle("Edit League");
		this.setDialogSize(new Dimension(300, 125));

		JPanel buttonPane = new JPanel();
		buttonPane.add(changeButton);
		buttonPane.add(closeButton);
		setButtonActions(this);

		JPanel formPane = new JPanel(new GridLayout(2, 2));
		JLabel leagueName = new JLabel("League Name: ");
		JLabel sportType = new JLabel("Sport Type: ");
		leagueField = new JTextField(25);
		sportField = new JTextField(25);

		

		formPane.add(leagueName);
		formPane.add(leagueField);
		formPane.add(sportType);
		formPane.add(sportField);

		Container contentPane = getContentPane();

		contentPane.add(formPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		pack();
	}
	
	public LeagueEditor(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		this(dialogOwner);
		this.model = model;
		this.selectedRow = row;
		// TODO Use enriqueDBmagic to make these textFields autoPopulate
		origLeague = (String) model.getValueAt(row, 0);
		origSport = (String) model.getValueAt(row, 1);
		leagueField.setText(origLeague);
		sportField.setText(origSport);
	}

	protected void setButtonActions(final AutoTeamDialog thisDialog)
	{
		changeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO change the currently selected league then close the
				// dialog
				// changeLeague( Enrique DBmagic );
				DataAccessObject dao = new DataAccessObject();
				ColumnData[] identifiers = { new ColumnData("leagueName",origLeague) };
				ColumnData[] changes = { new ColumnData("leagueName",leagueField.getText()),
										 new ColumnData("sport",sportField.getText()) };
				dao.updateRecord("leagues", identifiers, changes);
				model.fireTableDataChanged();
				thisDialog.close();
			}
		});

		closeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				thisDialog.close();
			}
		});
	}
}
